{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 下载历史数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from xtquant import xtdata\n",
    "res = xtdata.download_history_data(stock_code='600519.SH', period='1d', start_time='20241001', end_time='20241231', incrementally = None)\n",
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from xtquant import xtdata\n",
    "\n",
    "stock_list = xtdata.get_stock_list_in_sector('沪深300')\n",
    "\n",
    "def update_progress(progress):\n",
    "    '''进度条'''\n",
    "    bar_length = 40  # 进度条长度\n",
    "    block = int(round(bar_length * progress))\n",
    "    text = f\"\\r[{'#' * block + '-' * (bar_length - block)}] {progress:.2f}%\"\n",
    "    if progress < 1:\n",
    "        print(text, end='', flush=True)\n",
    "    else:\n",
    "        print(text, flush=True)\n",
    "        \n",
    "\n",
    "# 补充历史数据\n",
    "def download(stock_list):\n",
    "    \n",
    "    for index, stock_code in enumerate(stock_list):\n",
    "        xtdata.download_history_data(stock_code=stock_code, period='1d', incrementally=True)\n",
    "        update_progress((index+1) / len(stock_list))\n",
    "        \n",
    "\n",
    "download(stock_list)\n",
    "\n",
    "print('下载完成！')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from xtquant import xtdata\n",
    "\n",
    "stock_list = xtdata.get_stock_list_in_sector('沪深300')\n",
    "\n",
    "def callback (data):\n",
    "    print(data)\n",
    "    \n",
    "xtdata.download_history_data2(stock_list, period='1d', start_time='20241001', end_time='20241231', callback=callback,incrementally =True)\n",
    "\n",
    "print('下载完成！')\n",
    "\n",
    "xtdata.run()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 获取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from xtquant import xtdata\n",
    "res = xtdata.get_local_data(stock_list=['600519.SH', '600000.SH'], period='1d')\n",
    "res['600519.SH']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from xtquant import xtdata\n",
    "res = xtdata.get_local_data(field_list=['close'], stock_list=['600519.SH', '600000.SH'], period='1d')\n",
    "res['600519.SH']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from xtquant import xtdata\n",
    "res = xtdata.get_local_data(stock_list=['600519.SH', '600000.SH'], period='1d')\n",
    "res['600519.SH']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from xtquant import xtdata\n",
    "res = xtdata.get_local_data(stock_list=['600519.SH', '600000.SH'], period='1d', count=100)\n",
    "res['600519.SH']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from xtquant import xtdata\n",
    "res = xtdata.get_local_data(stock_list=['600519.SH', '600000.SH'], period='tick', start_time='20250101000000', end_time='20250110000000')\n",
    "res['600519.SH']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from xtquant import xtdata\n",
    "stock_code = '600519.SH'\n",
    "xtdata.download_history_data(stock_code=stock_code, period='1d')\n",
    "res = xtdata.get_local_data(stock_list=[stock_code], period='1d')\n",
    "res[stock_code]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from xtquant import xtdata\n",
    "res = xtdata.get_local_data(stock_list=['600519.SH'], period='1d', dividend_type='back')\n",
    "res['600519.SH']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from xtquant import xtdata\n",
    "res = xtdata.get_market_data(stock_list=['600519.SH', '600000.SH'], period='1d', start_time='20240101', end_time='20241231', dividend_type='none')\n",
    "\n",
    "res['time']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from xtquant import xtdata\n",
    "res = xtdata.get_market_data_ex(stock_list=['600519.SH', '600000.SH'], period='1d', start_time='20240101', end_time='20241231', dividend_type='none')\n",
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from xtquant import xtdata\n",
    "import os\n",
    "\n",
    "stock_list = xtdata.get_stock_list_in_sector('沪深300')\n",
    "\n",
    "def update_progress(progress):\n",
    "    '''进度条'''\n",
    "    bar_length = 40  # 进度条长度\n",
    "    block = int(round(bar_length * progress))\n",
    "    text = f\"\\r[{'#' * block + '-' * (bar_length - block)}] {progress * 100:.2f}%\"\n",
    "    if progress < 1:\n",
    "        print(text, end='', flush=True)\n",
    "    else:\n",
    "        print(text, flush=True)\n",
    "        \n",
    "\n",
    "# 补充历史数据\n",
    "def download(stock_list):\n",
    "    \n",
    "    for index, stock_code in enumerate(stock_list):\n",
    "        xtdata.download_history_data(stock_code=stock_code, period='1d', incrementally=True)\n",
    "        update_progress((index+1) / len(stock_list))\n",
    "        \n",
    "\n",
    "download(stock_list)\n",
    "    \n",
    "res = xtdata.get_local_data(stock_list=stock_list, period='1d')\n",
    "\n",
    "# 确保保存文件的目录存在\n",
    "output_dir = 'stock_data'\n",
    "if not os.path.exists(output_dir):\n",
    "    os.makedirs(output_dir)\n",
    "\n",
    "# 遍历res字典，将每个DataFrame保存为CSV文件\n",
    "for stock_code, df in res.items():\n",
    "    file_path = os.path.join(output_dir, f'{stock_code}.csv')\n",
    "    df.to_csv(file_path, index=False)\n",
    "    print(f'Saved {stock_code} data to {file_path}')\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
